package io.github.junxworks.qt.config;

import jakarta.annotation.PostConstruct;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class FlayWayConfig {
    private String SQL_LOCATION = "classpath:db/migration";

    private String VERSION_TABLE = "flyway_schema_history";

    private boolean BASELINE_ON_MIGRATE = true;

    private boolean OUT_OF_ORDER = false;

    private boolean VALIDATE_ON_MIGRATE = false;

    @Autowired
    @Qualifier("mainDataSource")
    private DataSource mainDataSource;

    @PostConstruct
    public void migrate() throws Exception {
        //dataSource可能被代理
        Flyway flyway = Flyway.configure()
                .dataSource(mainDataSource)
                .baselineOnMigrate(true)
                .baselineVersion("1")
                .locations(SQL_LOCATION)
                .baselineOnMigrate(BASELINE_ON_MIGRATE)
                .table(VERSION_TABLE)
                .outOfOrder(OUT_OF_ORDER)
                .validateOnMigrate(VALIDATE_ON_MIGRATE)
                .load();
        flyway.migrate();
    }
}
